package coder.nav.controller;

import coder.nav.common.JwtToken;
import coder.nav.common.R;
import coder.nav.model.bo.NavPageBO;
import coder.nav.model.bo.NavPageQueryBO;
import coder.nav.model.bo.NavTaskBO;
import coder.nav.model.bo.NavTaskQueryBO;
import coder.nav.model.vo.NavCustomVO;
import coder.nav.model.vo.NavPageVO;
import coder.nav.model.vo.NavTaskVO;
import coder.nav.service.NavPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Api(tags = "动态页面")
@RestController
@RequestMapping
public class NavPageController {

    @Autowired
    private NavPageService pageService;

    @ApiOperation("帮助手册")
    @GetMapping(value = "/readme")
    public ModelAndView readme() {
        return new ModelAndView(new RedirectView("https://gitee.com/bosen-once/coder-nav#1%E5%AF%BC%E8%88%AA%E4%B8%BB%E9%A1%B5"));
    }

    @ApiOperation("动态页面")
    @GetMapping(value = "/page/{userId}/{pageUrl}", produces = MediaType.TEXT_HTML_VALUE)
    public String page(@PathVariable("userId") Long userId, @PathVariable("pageUrl") String pageUrl) {
        return pageService.page(userId, pageUrl);
    }

    @ApiOperation("静态页面")
    @GetMapping(value = "/share/{userId}/{pageUrl}", produces = MediaType.TEXT_HTML_VALUE)
    public String share(@PathVariable("userId") Long userId, @PathVariable("pageUrl") String pageUrl) {
        return pageService.share(userId, pageUrl);
    }

    @JwtToken
    @ApiOperation("动态页面-保存")
    @PostMapping("/page/save")
    public R<Void> save(@Validated @RequestBody NavPageBO bo) {
        pageService.savePage(bo);
        return new R<>();
    }

    @JwtToken
    @ApiOperation("动态页面-删除")
    @DeleteMapping("/page/delete")
    public R<Void> delete(@RequestParam("id") Long id) {
        pageService.deletePage(id);
        return new R<>();
    }

    @JwtToken
    @ApiOperation("动态页面-分页")
    @PostMapping("/page/getPage")
    public R<NavPageVO<NavCustomVO>> getPage(@Validated @RequestBody NavPageQueryBO bo) {
        return new R<>(pageService.getPage(bo));
    }

    @JwtToken
    @ApiOperation("动态页面-详情")
    @GetMapping("/page/getInfo")
    public R<NavCustomVO> getInfo(@RequestParam("id") Long id) {
        return new R<>(pageService.getPageInfo(id));
    }

}
